<template>
	<view>
		<view class="selectbox">
			<view class="selectitem" :class="index == currentIndex ? 'active' : ''" v-for="(item, index) in typeList" :key="index" @click="changeIndex(index)">{{ item }}</view>
		</view>
		<view>
			<view class="musicbox" v-if="typeIndex[currentIndex] == 1">
				<view class="musicitem tabletitle">
					<view class="rank">序号</view>
					<view class="title">标题</view>
					<view class="singer">歌手</view>
					<view class="album">专辑</view>
					<view class="time">时长</view>
				</view>
				<view class="musicitem" v-for="(item, index) in songtList" :key="index+'-'+item.id" @click="playMusic(item.id)">
					<view class="rank">{{ index + 1 }}</view>
					<view class="title">{{ item.name }}</view>
					<view class="singer">{{ item.author }}</view>
					<view class="album">{{ item.album }}</view>
					<view class="time">{{ item.time | formDate }}</view>
				</view>
			</view>
			<view class="singerbox" v-else-if="typeIndex[currentIndex] == 100">
				<view class="singeritem" v-for="(item, index) in singerList" :key="index" @click="gotoSinger(item)">
					<view class="singerimage"><image class="" :src="item.picUrl" mode="widthFix" lazy-load="true" /></view>
					<view class="singername">{{ item.name }}</view>
				</view>
			</view>
			<view class="playlistbox" v-else-if="typeIndex[currentIndex] == 1000">
				<view class="playlistitem" v-for="(item, index) in playList" :key="index">
					<view class="" @click="goPlayList(item.id)">
						<view class="playlistimg">
							<image class="" :src="item.coverImgUrl" mode="widthFix" />
						</view>
						<view class="playlistname">{{ item.name }}</view>
					</view>
				</view>
			</view>
			<view class="mvbox" v-else-if="typeIndex[currentIndex] == 1004">
					<view class="mvitem" v-for="(item, index) in mvList" :key="index">
						<view class="mvimg" @click="gotoMv(item.id)">
							<image class="" :src="item.cover" mode="widthFix" lazy-load="true" />
						</view>
						<view class="mvname">{{ item.name }}</view>
						<view class="mvcount">播放量：{{ item.playCount }}</view>
					</view>
				</view>
		</view>
	</view>
</template>

<script>
import { searchRequest } from '../../api/search.js';
export default {
	data() {
		return {
			typeList: ['音乐', '歌手', '歌单', 'MV'],
			typeIndex: [1, 100, 1000, 1004],
			currentIndex: 0,
			keywords: '',
			songtList: [],
			singerList: [],
			playList: [],
			mvList: []
		};
	},
	onLoad(option) {
		this.keywords = option.keyword;
		this.getResult();
	},
	filters: {
		formDate(date) {
			var m = parseInt(date / 1000 / 60);
			if (m < 10) {
				m = '0' + m;
			}
			var s = parseInt((date / 1000) % 60);
			if (s < 10) {
				s = '0' + s;
			}
			return m + ':' + s;
		},
		count(playcount) {
			if (playcount >= 10000) {
				playcount = Math.ceil(playcount / 10000) + '万';
			}
			return playcount;
		}
	},
	methods: {
		async getResult() {
			let type = this.typeIndex[this.currentIndex];
			let result = await searchRequest({
				url: '/search',
				data: {
					keywords: this.keywords,
					type
				}
			});

			if (type == 1) {
				var list = result[1].data.result.songs.map(item => {
					return {
						name: item.name,
						author: item.artists[0].name,
						album: item.album.name,
						time: item.duration,
						id:item.id
					};
				});
				this.songtList.push(...list);
			} else if (type == 100) {
				this.singerList.push(...result[1].data.result.artists);
			} else if (type == 1000) {
				this.playList.push(...result[1].data.result.playlists);
			} else {
				this.mvList.push(...result[1].data.result.mvs);
			}
		},
		changeIndex(index) {
			this.currentIndex = index;
			this.getResult();
		},
		playMusic(id) {
			// uni.navigateTo({
			// 	url: '/pages/music/music?id=' + id
			// });
			this.$store.dispatch('getSong', id);
		},
		goPlayList(id) {
			uni.navigateTo({
				url: '/pages/playlist/playlist?id=' + id
			});
		},
		gotoSinger(singer){
			uni.navigateTo({
				url: '/pages/singer/singer?id='+singer.id 
			});
			// uni.$emit("getsinger",singer)
			uni.setStorageSync("singer",singer);
		},
		gotoMv(id){
			let type = 5;
			uni.navigateTo({
				url: '/pages/mv/mv?id='+id+"&type="+type 
			});
		}
	}
};
</script>

<style lang="scss">
.selectbox {
	margin: 0upx 10upx;
	display: flex;
	.selectitem {
		flex: 1;
		text-align: center;
		height: 90upx;
		line-height: 90upx;
	}
}
.active {
	color: $uni-bg-color;
	border-bottom: 4upx solid $uni-bg-color;
}
.musicitem {
	margin: 10upx;
	margin-bottom: 20upx;
	height: 60upx;
	line-height: 60upx;
	display: flex;
	font-size: 26upx;
	view {
		overflow: hidden;
		text-align: center;
		margin: 0upx 5upx;
	}
	.rank {
		flex: 1;
	}
	.title {
		flex: 3;
	}
	.singer {
		flex: 2;
	}
	.album {
		flex: 4;
	}
	.time {
		flex: 2;
	}
}
.singerbox {
	display: flex;
	flex-wrap: wrap;
	margin-bottom: 20upx;
}

.singerbox .singeritem {
	width: 33.33%;
	margin-top: 30upx;
}

.singerbox .singeritem .singerimage {
	height: 170upx;
	overflow: hidden;
}

.singerbox .singeritem .singerimage image {
	width: 80%;
	height: auto;
	margin-left: 10%;
	border-radius: 10upx;
}

.singerbox .singeritem .singername {
	font-size: 26upx;
	width: 100%;
	text-align: center;
}
.playlistbox {
	display: flex;
	flex-wrap: wrap;
	margin-top: 20upx;
	margin-bottom: 20upx;
}

.playlistbox .playlistitem {
	width: 33.33%;
}

.playlistbox .playlistitem .playlistimg {
	width: 90%;
	position: relative;
}

.playlistbox .playlistitem .playlistimg image {
	width: 100%;
	margin-left: 5%;
	border-radius: 10upx;
	height: auto;
}

.playlistbox .playlistitem .playlistimg .playlistcount {
	width: 100%;
	position: absolute;
	top: 10upx;
	right: 10upx;
	color: white;
	text-align: right;
	vertical-align: middle;
}

.playlistbox .playlistitem .playlistimg .playlistcount image {
	width: 25upx;
	height: auto;
	vertical-align: middle;
	display: inline-block;
}

.playlistbox .playlistitem .playlistimg .playlistcount .tricontent {
	font-size: 22upx;
	display: inline-block;
	vertical-align: middle;
}

.playlistbox .playlistitem .playlistname {
	font-size: 26upx;
	margin: 0px 20upx 20upx 20upx;
	display: -webkit-box;
	-webkit-box-orient: vertical;
	-webkit-line-clamp: 2;
	overflow: hidden;
}
.mvbox {
	display: flex;
	flex-wrap: wrap;
	margin-top: 20upx;
}
.mvbox .mvitem {
	width: 50%;
	margin-bottom: 20upx;
}

.mvbox .mvitem  .mvimg {
	width: 96%;
	overflow: hidden;
	position: relative;
	margin: 0 2%;
}

.mvbox .mvitem  .mvimg image {
	width: 100%;
	height: auto;
}

.mvbox .mvitem  .mvimg .bgimgbox {
	position: absolute;
	top: 0;
	left: 0;
	right: 0;
	bottom: 0;
	display: flex;
	justify-content: center;
	align-items: center;
}
.mvbox .mvitem  .mvimg .bgimgbox image {
	width: 100upx;
	height: auto;
}

.mvbox .mvitem .mvname {
	height: 40upx;
	line-height: 40upx;
	margin: 0upx 20upx;
	display: -webkit-box;
	-webkit-box-orient: vertical;
	-webkit-line-clamp: 1;
	overflow: hidden;
	font-size: 26upx;
}

.mvbox .mvitem .mvcount {
	margin: 10upx 20upx;
	font-size: 25upx;
	color: #ccc;
}
</style>
